package com.example.yichuangyun.utils.producttreelist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.SetShareItemBinding;

import java.util.List;

/**
 * 意向产品适配器
 */
public class ProductTreeAdapter extends ProductTreeRecyclerAdapter {

    private OnCheckListener onCheckListener;
    private final List<ProductNodeModel> data;
    private final int flag;

    public ProductTreeAdapter(Context context, List<ProductNodeModel> data, int defaultExpandLevel, int flag) {
        super(context, data, defaultExpandLevel);
        this.data = data;
        this.flag = flag;
    }

    @Override
    public void onBindViewHolder(ProductNodeModel node, RecyclerView.ViewHolder holder, int position) {
        final MyViewHolder viewHolder = (MyViewHolder) holder;
        viewHolder.binding.rlItem.setOnClickListener(v -> {
            if (node.getUseState() == 1) {
                expandOrCollapse(position);
            } else {
                // flag=1商机来源，0意向产品
                if (flag == 0) {
                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).getFlag() != node.getFlag()) {
                            data.get(i).setChecked(false);
                        }
                    }
                    node.setChecked(!node.isChecked());
                } else {
                    if (node.isChecked()){
                        node.setChecked(false);
                    } else {
                        for (ProductNodeModel nodeModel : mAllNodes) {
                            nodeModel.setChecked(false);
                        }
                        node.setChecked(!node.isChecked());
                    }
                }
                notifyDataSetChanged();
                if (onCheckListener != null) {
                    onCheckListener.onClick(node);
                }
            }
        });

        viewHolder.binding.rlItem.setBackgroundColor(node.isChecked() ? 0xFFDAE3FF : 0xFFFFFFFF);
        viewHolder.binding.ivSelectTree.setImageResource(node.isChecked() ? R.mipmap.icon_choose_square : R.mipmap.icon_nochoose_square);
        viewHolder.binding.ivSelectTree.setVisibility(node.getUseState() == 1 ? View.GONE : View.VISIBLE);
        if (node.getIcon() == -1) {
            viewHolder.binding.icon.setVisibility(View.GONE);
        } else {
            viewHolder.binding.icon.setVisibility(View.VISIBLE);
            viewHolder.binding.icon.setImageResource(node.getIcon());
        }
        viewHolder.binding.tvTreeNodeLabel.setText(node.getTitle());
        // 编辑商机时不能修改意向产品大类，其他产品文字置灰，checkBox不展示
        viewHolder.binding.tvTreeNodeLabel.setTextColor(node.getUseState() == 1 ? 0xFF999999 : 0xFF3B3A40);
        viewHolder.binding.icon.setOnClickListener(view -> expandOrCollapse(position));
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        SetShareItemBinding itemBinding = SetShareItemBinding.inflate(LayoutInflater.from(mContext), parent, false);
        return new MyViewHolder(itemBinding);
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        SetShareItemBinding binding;

        public MyViewHolder(SetShareItemBinding itemBinding) {
            super(itemBinding.getRoot());
            binding = itemBinding;
        }
    }

    public interface OnCheckListener {
        void onClick(ProductNodeModel productNodeModel);
    }

    public void setOnOnCheckListener(OnCheckListener onCheckListener) {
        this.onCheckListener = onCheckListener;
    }
}
